﻿// <copyright file="App.xaml.cs" company="Mooville">
//   Copyright © 2010 Roger Deetz. All rights reserved.
// </copyright>

namespace Mooville.QUno.Phone
{
    using System.Diagnostics;
    using System.Windows;
    using System.Windows.Navigation;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;

    public partial class App : Application
    {
        private bool phoneApplicationInitialized = false;

        public App()
        {
            this.UnhandledException += this.Application_UnhandledException;

            this.InitializeComponent();
            this.InitializePhoneApplication();

            if (Debugger.IsAttached)
            {
                Application.Current.Host.Settings.EnableFrameRateCounter = true;

                // Show the areas of the app that are being redrawn in each frame.
                ////Application.Current.Host.Settings.EnableRedrawRegions = true;

                // Enable non-production analysis visualization mode, 
                // which shows areas of a page that are handed off to GPU with a colored overlay.
                ////Application.Current.Host.Settings.EnableCacheVisualization = true;

                PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
            }
        }

        public PhoneApplicationFrame RootFrame { get; private set; }

        private void InitializePhoneApplication()
        {
            if (this.phoneApplicationInitialized)
            {
                return;
            }

            this.RootFrame = new PhoneApplicationFrame();
            this.RootFrame.Navigated += this.CompleteInitializePhoneApplication;
            this.RootFrame.NavigationFailed += this.RootFrame_NavigationFailed;
            this.phoneApplicationInitialized = true;

            return;
        }

        private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
        {
            if (this.RootVisual != this.RootFrame)
            {
                this.RootVisual = this.RootFrame;
            }

            this.RootFrame.Navigated -= this.CompleteInitializePhoneApplication;

            return;
        }

        private void Application_Launching(object sender, LaunchingEventArgs e)
        {
            return;
        }

        private void Application_Closing(object sender, ClosingEventArgs e)
        {
            return;
        }

        private void Application_Activated(object sender, ActivatedEventArgs e)
        {
            return;
        }

        private void Application_Deactivated(object sender, DeactivatedEventArgs e)
        {
            return;
        }

        private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            if (Debugger.IsAttached)
            {
                Debugger.Break();
            }

            return;
        }

        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            if (Debugger.IsAttached)
            {
                Debugger.Break();
            }

            return;
        }
    }
}
